import Ball from "./Ball";
import GunControl from "./GunControl";

/**
*
* @ author:JiangChencheng
* @ email:744578095@qq.com
* @ data: 2021-10-27 14:50
*/
export default class ShootManager extends Laya.Script {

    constructor() {
        super();
        /** @prop {name:gun, tips:"发射器", type:Node, default:null}*/
        this.gun = null
        /** @prop {name:ballPrefab, tips:"小球预制体", type:Prefab, default:null}*/
        this.ballPrefab = null
        /** @prop {name:shootCountTxt, tips:"发射数量文本", type:Node, default:null}*/
        this.shootCountTxt = null
        /** @prop {name:speedUpTxt, tips:"是否加速文本", type:Node, default:null}*/
        this.speedUpTxt = null

        this.isMouseDown = false
        // 发射小球的计数器
        this.shootCount = 0
        // 一个有多少个小球可供发射
        this.canShootCount = 5
        // 是否可以发射
        this.canShoot = true
        // 回收小球的数量，回收数量>发射小球的数量时，才可以发射小球
        this.revcoverCount = 0

        this.addCount = 0

        this.gunController = null
    }

    onAwake() {

        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown)
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.onMouseUp)
        Laya.stage.on(Laya.Event.MOUSE_OUT,this,this.onMouseOut)
        Laya.stage.on(Laya.Event.MOUSE_MOVE,this,this.onMouseMove)

        this.shootCountTxt.text = this.canShootCount

        Laya.stage.on("RecoverBall",this,this.recoverBall)
        Laya.stage.on("AddShootCount",this,this.addShootCount)

        this.gunController = this.gun.parent.getComponent(GunControl)
    }

    onDestroy(){
        Laya.stage.off("RecoverBall",this,this.recoverBall)
        Laya.stage.off("AddShootCount",this,this.addShootCount)
    }

    onMouseDown(e) {
        
        if (e.target.name == "btn_left" || e.target.name == "btn_right"
        || e.target.name == "top" || e.target.name == "ball_count"
        || e.target.name == "addBtn") return
        if (this.canShoot == false) return
        this.isMouseDown = true
        this.lookAtMouse()
    }

    onMouseUp(e) {
        if (e.target.name == "btn_left" || e.target.name == "btn_right") return
        if (this.canShoot == false) return
        if (this.isMouseDown == false) return 
        this.isMouseDown = false
        this.canShoot = false
        // 隐藏左右切换按钮
        this.gunController.hideChangeButton(true)
        this.shootCount = this.canShootCount
        var deltaX = Laya.stage.mouseX - this.gun.x
        var deltaY = Laya.stage.mouseY - this.gun.y
        // 鼠标抬起立即发射一个球
        this.shootBall(deltaX,deltaY)
        if (this.shootCount > 0) {
            // 循环定时射击
            Laya.timer.loop(300,this,this.loopShootBall,[deltaX,deltaY])
        }
        // 开启延时，2秒后加速
        Laya.timer.once(1000,this,this.speedUp)
    }

    speedUp(){
        this.speedUpTxt.visible = true
        Laya.timer.scale = 3
    }

    onMouseOut(){
        if (this.canShoot == false) return
        this.isMouseDown = false
    }


    onMouseMove() {
        if (this.canShoot == false) return
        // 按住时移动才能根据鼠标调整发射器的角度
        if(this.isMouseDown) {
            this.lookAtMouse()
        }
    }

    // 点击或者按住鼠标移动时改变发射器方向
    lookAtMouse() {
        var deltaX = Laya.stage.mouseX - this.gun.x
        var deltaY = Laya.stage.mouseY - this.gun.y
        var degree = -Math.atan2(deltaX, deltaY)*180/Math.PI
        this.gun.rotation = degree
    }

    // 发射一个小球
    shootBall(deltaX,deltaY){
        var ball = Laya.Pool.getItemByCreateFun("Ball",function(){
           return this.ballPrefab.create()
        },this)
        this.owner.addChild(ball)
        ball.visible = true
        ball.active = true
        ball.pos(this.gun.x,this.gun.y)
        ball.getComponent(Laya.RigidBody).type = "dynamic"
        ball.getComponent(Laya.RigidBody).linearVelocity = {x:deltaX, y:deltaY}
        var ballType = this.gunController.ballType

        ball.texture = this.gunController.ballUrlArr[ballType]
        var ballCtroller =  ball.getComponent(Ball)
        if(ballType == 1) {
            ballCtroller.damage = 3
            ballCtroller.damageRow = false
        }else if(ballType == 2) {
            ballCtroller.damage = 2
            ballCtroller.damageRow = true
        }else {
            ballCtroller.damage = 1
            ballCtroller.damageRow = false
        }
        this.gunController.reduceBallCount()
        this.shootCount--
        this.shootCountTxt.text = this.shootCount
    }
    // 循环发射小球
    loopShootBall(deltaX,deltaY){
        if (this.shootCount <= 0) {
            Laya.timer.clear(this,this.loopShootBall)
        }else {
            this.shootBall(deltaX,deltaY)
        }
    }
    // 回收小球的回调
    recoverBall(){
        this.revcoverCount++
        if (this.revcoverCount >= this.canShootCount) {

            Laya.timer.clear(this,this.speedUp)
            Laya.timer.scale = 1
            this.speedUpTxt.visible = false
             // 显示左右切换按钮
            this.gunController.hideChangeButton(false)
            this.revcoverCount = 0
            this.canShoot = true
            this.canShootCount+=this.addCount
            this.addCount = 0
            this.shootCountTxt.text = this.canShootCount
            Laya.stage.event("CreateNextRow")
        }
    }

    addShootCount(){
        this.addCount++
    }
}